﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace One_day.Controllers
{
    public class UserController : Controller
    {
        // GET: User
        /// <summary>
        /// 注册界面
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult Regest()
        {

            return View();
        }
        public ActionResult RegestSave(String LoginName, String Password, String RealName, String Email, int type = 1)
        {
            One_day.Models.T_Base_User user = new One_day.Models.T_Base_User();
            user.LoginName = LoginName;
            user.PWD = Password;
            user.RealName = RealName;
            user.Email = Email;
            user.Type = type;
            One_day.Models.YichenEntities entities = new Models.YichenEntities();
            ////将新增的数据加入对应表中，更新数据
            entities.T_Base_User.Add(user);
            entities.SaveChanges();

            return View();
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 账户检测
        /// </summary>
        /// <returns></returns>
        public JsonResult LoginCheck(String LoginName,String PWD)
        {
            //ajax方法

            //账号密码正确
            One_day.Models.YichenEntities entity = new One_day.Models.YichenEntities();
            if (entity.T_Base_User.Where(m => m.LoginName == LoginName && m.PWD == PWD&&m.Type==1).ToList().Count > 0)
            {
                One_day.Models.T_Base_User user=entity.T_Base_User.Where(m => m.LoginName == LoginName && m.PWD == PWD&&m.Type==1).ToList()[0];
                //Session  浏览器开启的情况下，数据共享
                Session["ticket"] = user;
                return Json(new { code = 1, message = "成功登录" });
            }
            else
                return Json(new { code = 0, message = "账号或密码错误" });


        }
        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        public ActionResult LoginOut()
        {
            //只需要把Session 置为空
            Session["ticket"] = null;
            return RedirectToAction("Index", "Yichen");
        }

        public ActionResult Person_infor()//个人中心，图标界面跳入
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            One_day.Models.T_Base_User user = (One_day.Models.T_Base_User)Session["ticket"];
            //没有登陆跳转到登陆界面，处于登陆状态则显示具体信息
            if (user == null)
                return Redirect("/user/login");
            else
                ViewBag.user = db.T_Base_User.Single(m => m.Id == user.Id);
            return View();
        }

        public ActionResult Address(string address)
        {
            //通过  ‘/’分隔各个收货地址
            String[] ad = address.Split('/');
            ViewBag.address = ad;
            return View();
        }

        public ActionResult Personal_Infor()//个人中心数据编辑修改
        {
            return View();
        }

        public ActionResult Personal_Infor_save(String RealName, String PhoneNumber)
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            One_day.Models.T_Base_User user = (One_day.Models.T_Base_User)Session["ticket"];
            One_day.Models.T_Base_User user_find = db.T_Base_User.Single(m => m.Id == user.Id);
            user_find.RealName = RealName;
            user_find.PhoneNumber = PhoneNumber;
            db.SaveChanges();
            return View();
        }
    }
}